/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Designation;

import java.sql.PreparedStatement;
import connectDB.ConnectDB;
import java.sql.*;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author ADMIN
 */
public class Des_Update extends javax.swing.JFrame {

    ConnectDB connect = new ConnectDB();
    String id;
    String name;

    public Des_Update(String id) {
        connect.getConnect();
        initComponents();
        this.id = id;
        this.name = name;
        loadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtDesIdUpdate = new javax.swing.JTextField();
        txtDesNameUpdate = new javax.swing.JTextField();
        btnDesUpdate = new javax.swing.JButton();
        btnDesCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtDesIdUpdate.setEnabled(false);

        txtDesNameUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDesNameUpdateActionPerformed(evt);
            }
        });

        btnDesUpdate.setText("Update");
        btnDesUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesUpdateActionPerformed(evt);
            }
        });

        btnDesCancel.setText("Cancel");
        btnDesCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(88, 88, 88)
                .addComponent(btnDesUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnDesCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(74, 74, 74))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 117, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtDesIdUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDesNameUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(103, 103, 103))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(txtDesIdUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtDesNameUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDesUpdate)
                    .addComponent(btnDesCancel))
                .addContainerGap(153, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtDesNameUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDesNameUpdateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDesNameUpdateActionPerformed

    private void btnDesCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnDesCancelActionPerformed

    private void btnDesUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesUpdateActionPerformed
        // TODO add your handling code here:    
        updateDes();
        
    }//GEN-LAST:event_btnDesUpdateActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDesCancel;
    private javax.swing.JButton btnDesUpdate;
    private javax.swing.JTextField txtDesIdUpdate;
    private javax.swing.JTextField txtDesNameUpdate;
    // End of variables declaration//GEN-END:variables

    private void updateDes() {


        int i = JOptionPane.showConfirmDialog(this, "Are you sure?", "Yes or No", 0);
        if (i == JOptionPane.YES_OPTION) {
            try {
                String query = "UPDATE Designation "
                        + "SET DESIGNID = ?, DESIGNATION = ? "
                        + "WHERE DESIGNID = ? ";
                PreparedStatement pst = connect.cn.prepareStatement(query);

                pst.setString(1, txtDesIdUpdate.getText());
                pst.setString(2, txtDesNameUpdate.getText());
                pst.setString(3, id);
                if (txtDesNameUpdate.getText().equals("")) {
                    JOptionPane.showMessageDialog(this, "Name not blank");
                    return;
                } else {

                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(this, "Update success");
                    this.dispose();
                    

                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Update fail");
                e.printStackTrace();
            }
        }else {
            JOptionPane.showMessageDialog(this, "Please choose the object that you want to update, again!!!");
            this.dispose();
        }

    }

    private void loadData() {
        try {
            String query1 = "Select * from Designation where DESIGNID = ?";
            PreparedStatement pst = connect.cn.prepareStatement(query1);
            pst.setString(1, id);

            ResultSet rs = pst.executeQuery();
            rs.next();
            txtDesIdUpdate.setText(rs.getString("DESIGNID"));
            txtDesNameUpdate.setText(rs.getString("DESIGNATION"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
